package com.wwfish.cmsui.dashboard.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.StackPanel;
import com.nexustar.gwt.dashboard.client.*;
import com.nexustar.gwt.widgets.client.PropertiesFactory;
import com.nexustar.gwt.widgets.client.asyn.FishAsyncCallback;
import com.nexustar.gwt.widgets.client.ui.tab.FishTabItem;
import com.nexustar.gwt.widgets.client.ui.tab.FishTabPanel;
import com.nexustar.gwt.widgets.client.ui.window.Message;
import com.wwfish.cms.model.UserDto;
import com.wwfish.cms.service.UserManager;
import com.wwfish.cmsui.modules.common.client.util.ServiceFactory;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2010-7-10
 * Time: 15:27:42
 * To change this template use File | Settings | File Templates.
 */
public class CMSDashboard implements EntryPoint {
    public static UserDto user;

    public void onModuleLoad() {
        initialiseViews(null);
    }


    private ViewPanel frame;
    private static FishTabPanel tabPanel;
    public static String _UPLOAD_IMAGE_PATH =
            PropertiesFactory.getInstance().getValueByKey("dash-properties.xml", "modelName") + "/servlet.gupld";
    public static String _DOWN_IMAGE_PATH =
            PropertiesFactory.getInstance().getValueByKey("dash-properties.xml", "modelName") + "/downImage/";


    public static FishTabPanel getTabPanel() {
        return tabPanel;
    }

    public void initialiseViews(String s) {


        frame = new ViewPanel();
        fetchLoginUser();
        RootPanel.get().add(frame);
    }

//    private void initFrame(UUser user) {

    private void initFrame() {
        //initNorthBar
        frame.setWelcomeBar(getWelcomeBar(user.getName()));

        //initCenter
        SimplePanel center = frame.getCenter();
        tabPanel = new FishTabPanel();
        final String url = PropertiesFactory.getInstance().getValueByKey("dash-properties.xml", "index");
        final Frame index = new Frame(url);
        index.setSize("100%", "100%");
        FishTabItem wel = new FishTabItem(tabPanel, index);
        wel.setText(PropertiesFactory.getInstance().getValueByKey("dash-properties.xml", "indextext"));
        wel.setClose(false);
        tabPanel.addTabItem(wel);
        tabPanel.setSelectItem(wel);
        wel.setIEvent(new FishTabItem.FishTableItemEvent() {
            public void onSelect() {

            }

            public void onRefresh() {
                index.setUrl(url);

            }

            public void onClose() {
//                Window.alert("onrefresh!");
            }
        });
        center.add(tabPanel);

        //initMenu
        StackPanel menu = frame.getMenuPanel();
        DashMenuBuilder.constructMenu(menu);
//       DOM.setElementAttribute( RootPanel.get("loading").getElement(),"display","none");
    }

    public void dispatch(String s, String s1) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    private String getWelcomeBar(String name) {
        StringBuffer s = new StringBuffer();
        s.append(name + ",");
//        s.append(getConstant("welcome"));
        s.append("欢迎使用！");
//        s.append(getConstant("exit"));
        s.append("<a href=\"" + PropertiesFactory.getInstance().getValueByKey("dash-properties.xml", "exit") + "\">退出</a>");
        s.append("&nbsp;&nbsp;");
//        s.append(getConstant("relogin"));
        s.append("<a href=\"" + PropertiesFactory.getInstance().getValueByKey("dash-properties.xml", "relogin") + "\">重新登录</a>");
        return s.toString();
    }

    private void fetchLoginUser() {
        ServiceFactory.invoke(UserManager.class.getName(), "authenticateUser", null, new FishAsyncCallback() {
            public void onSuccess(Object o) {
                user = (UserDto) o;
                initFrame();
            }
        });

    }

    public static void dispatchPage(final String pageId, final PageClient pageClient) {

        if (pageId.indexOf(DashMenuBuilder._PAGE_MOCK_FLAG) != -1) {
            String realPageId = pageId.substring(pageId.indexOf(DashMenuBuilder._PAGE_MOCK_FLAG) + 1);
            PagesFactory.getInstance().getPage(pageId, realPageId, getPageClient(pageId, pageClient));
        } else
            PagesFactory.getInstance().getPage(pageId, getPageClient(pageId, pageClient));

    }

    public static PageClient getPageClient(final String pageId, final PageClient pageClient) {
        return new PageClient() {
            public void success(AbstractPage page) {
                PageModel pageModel = (PageModel) DashMenuBuilder.getPageModelMap().get(pageId);
                if (pageModel != null)
                    DashMenuBuilder.dispatchPage(pageModel, false, pageClient);
                else {
                    /**
                     * 对于那些没有注册在功能节点上的Page，则在选中的tableItem上面 加载。请确保加载页面和MenuModel一致性。
                     */
                    if (page != null) {
                        final FishTabItem item = CMSDashboard.getTabPanel().getSelectItem();
                        PageModel model = (PageModel) DashMenuBuilder.getPageModelMap().get(item.getUserObject().toString());
                        page.setMenuModel(model.getMenuModel());
                        item.setShowWidget(page);
                        tabPanel.setSelectItem(item);
                        if (page.getPageTitle() != null)
                            setSelectedItemTitle(page.getPageTitle());
                        if (pageClient != null)
                            pageClient.success(page);
                    } else {
                        Window.alert("pageId is valid!:NO page can be matched" + pageId);
                        return;
                    }
                }


            }

            public void failure() {
                pageClient.failure();
            }
        };
    }

    public static void setSelectedItemTitle(String title) {
        tabPanel.getSelectItem().setText(title);
    }


}
